package com.hao.wms.service.impl;

import com.hao.wms.dao.IEmployeeDao;
import com.hao.wms.domain.Employee;
import com.hao.wms.domain.Permission;
import com.hao.wms.domain.Role;
import com.hao.wms.query.PageResult;
import com.hao.wms.query.QueryObject;
import com.hao.wms.service.IEmployeeService;
import com.hao.wms.util.UserContext;
import com.opensymphony.xwork2.ActionContext;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class EmployeeServiceImpl implements IEmployeeService {
	private IEmployeeDao dao;

	public void setDao(IEmployeeDao dao) {
		this.dao = dao;
	}

	@Override
	public void save(Employee e) {
		dao.save(e);

	}

	@Override
	public void update(Employee e) {
		dao.update(e);

	}

	@Override
	public void delete(Employee e) {
		dao.delete(e);

	}

	@Override
	public Employee get(Long id) {
		return dao.get(id);
	}

	@Override
	public List<Employee> list() {
		return dao.list();
	}

	@Override
	public PageResult query(QueryObject qo) {
		return dao.query(qo);
	}

	@Override
	public Employee login(String name, String password) {
		Employee emp = dao.login(name, password);
		if (emp != null) {
			UserContext.setUser(emp);
			Set<String> permissions = new HashSet<>();
			for (Role r : emp.getRoles()) {
				for (Permission p : r.getPermissions()) {
					permissions.add(p.getExpression());
				}
			}
			UserContext.setPermission(permissions);
		}else {
			throw new RuntimeException("账号或者密码有误");
		}
		return emp;
	}

	@Override
	public void batchDelete(List<Long> ids) {
		dao.batchDelete(ids);
	}
}
